﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OZDoo.Core.Log;

namespace OZDoo.WebCommon.WebControls
{
    /// <summary>
    /// Summary description for Pager.
    /// </summary>
    public class Pager: TemplatedWebControl,  IPagedControl
    {
        HyperLink firstpage;
        HyperLink prepage;
        HyperLink nextpage;
        HyperLink lastpage;
        Label pageview;
        DropDownList pagelist;

        #region IPagedControl 成员
        private int pageIndex;
        public int PageIndex
        {
            get
            {
                if(pageIndex>0)
                   return pageIndex;
                if (!Globals.IsNullorEmpty(this.Page.Request.QueryString["PageIndex"]))
                {
                    pageIndex = Globals.SafeInt(this.Page.Request.QueryString["PageIndex"], 0);
                }                
                if (totalPageCount >0 && pageIndex > totalPageCount)
                    pageIndex = totalPageCount;
                if (pageIndex < 1)
                    pageIndex = 1;
                return pageIndex;
            }
            set
            {
                pageIndex = value;
            }
        }
        private int totalPageCount;
        public int TotalPageCount
        {
            get
            {
                if(totalPageCount>0)
                    return totalPageCount;
                if (totalPageCount < 0)
                    totalPageCount = 0;
                
                return totalPageCount;
            }
            set
            {
                totalPageCount = value;
            }
        }
        private int totalCount;
        public int TotalCount
        {
            get
            {
                if (totalCount > 0)
                    return totalCount;
                if (totalCount < 0)
                    totalCount = 0;
                return totalCount;
            }
            set
            {
                totalCount = value;
            }
        }
        private int pageSize;
        public int PageSize
        {
            get
            {
                if (pageSize > 0)
                    return pageSize;
                if (pageSize < 0)
                    pageSize = 0;
                return pageSize;
            }
            set
            {
                pageSize = value;
            }
        }


        private string urlPattern;

        public string UrlPattern
        {
            get { return urlPattern; }
            set { urlPattern = value; }
        }


        #endregion

        public override void DataBind()
        {
           
            base.DataBind();
            firstpage.NavigateUrl = CreatePagerURL((1).ToString());
            if (PageIndex > 1)
            {
                prepage.NavigateUrl = CreatePagerURL((PageIndex - 1).ToString());
            }
            if (PageIndex < totalPageCount)
            {
                nextpage.NavigateUrl = CreatePagerURL((PageIndex + 1).ToString());
            }
            if (pagelist != null)
            {
                int[] pageAry = {1};
                if(totalPageCount>0)
                   pageAry=new int[totalPageCount];
                for (int i = 0; i <totalPageCount; i++)
                {
                    pageAry[i] = i + 1;                   
                }
                pagelist.DataSource = pageAry;
                pagelist.DataBind();
            }
            lastpage.NavigateUrl = CreatePagerURL(totalPageCount.ToString());
            pageview.Text = "第" + pageIndex.ToString() + "页  " + PageSize.ToString() + "/页 共" + this.totalCount + "条记录，共" + TotalPageCount.ToString() + "页";
        }

        protected override void AttachChildControls()
        {           
            firstpage = FindControl("firstpage") as HyperLink;
            prepage = FindControl("prepage") as HyperLink;
            nextpage = FindControl("nextpage") as HyperLink;
            lastpage = FindControl("lastpage") as HyperLink;
            pageview = FindControl("pageview") as Label;
            pagelist = FindControl("pagelist") as DropDownList;
            pagelist.DataBound += new EventHandler(pagelist_DataBound);
                      
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            DataBind();  
        }


        void pagelist_DataBound(object sender, EventArgs e)
        {
            DropDownList list = sender as DropDownList;
            if (list != null)
            {
                ListItem item=list.Items.FindByValue(PageIndex.ToString());
                if (item != null)
                {
                    list.SelectedIndex = -1;
                    item.Selected=true;
                }
            }
        }

      
        #region Helper methods and Public Properties
        protected virtual string CreatePagerURL(string pageIndex)
        {
            string url;
            if (Globals.IsNullorEmpty(urlPattern))
                url = this.Page.Request.Url.ToString();
            else
                return string.Format(UrlPattern, pageIndex);

            if (url.IndexOf("?") == -1)
            {
                return url + "?PageIndex=" + pageIndex;
            }
            else
            {
                if (url.IndexOf("PageIndex=") == -1)
                    return url + "&PageIndex=" + pageIndex;
                else
                {
                    return Regex.Replace(url, @"PageIndex=(\d+\.?\d*|\.\d+)", "PageIndex=" + pageIndex);
                }
            }
        }
        #endregion
    }

   
}
